package com.qxsx.midi.activity.study;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.qxsx.midi.R;
import com.qxsx.midi.activity.base.BaseActivity;
import com.qxsx.midi.utils.MyNet;
import com.qxsx.midi.utils.callBackJsonArray;

import net.tsz.afinal.FinalBitmap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class StudyClubDetailActivity extends BaseActivity implements OnClickListener {

    private static String _id;//ID

    private String _tel;//Tel

    private PopupWindow popupWindow;

    private ImageView iv_imag;

    private TextView tv_name, tv_desc;

    private FinalBitmap finalImageLoader;

    private WebView wv_myWebView;
    private String _url;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_study_where_detail);
        setTitle("midi club 详情");
        setNavBtn(R.mipmap.back, R.mipmap.study_mandetail_phone);
        Intent intent = getIntent();
        _id = intent.getStringExtra("id");
        finalImageLoader = FinalBitmap.create(this);
        finalImageLoader.configLoadingImage(R.mipmap.binner);
        finalImageLoader.configLoadfailImage(R.mipmap.binner);
        findById();
        inItData();

    }

    private void inItData() {
        information();

        webv();

    }


    private void webv(){
        //支持javascript
        wv_myWebView.getSettings().setJavaScriptEnabled(true);
        // 设置可以支持缩放
        wv_myWebView.getSettings().setSupportZoom(true);
        // 设置出现缩放工具
        wv_myWebView.getSettings().setBuiltInZoomControls(true);
        //扩大比例的缩放
        wv_myWebView.getSettings().setUseWideViewPort(true);
        //自适应屏幕
        wv_myWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        wv_myWebView.getSettings().setLoadWithOverviewMode(true);
        wv_myWebView.getSettings().setTextSize(WebSettings.TextSize.LARGEST);

        JSONArray array = new JSONArray();
        JSONObject object = new JSONObject();
        try {
            object.put("site_id", _id+"");
            array.put(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        MyNet.doPostJsonArray("551", array, new callBackJsonArray() {
            @Override
            public void run(Object code, Object message, JSONArray array) {
                try {
                    if ("200".equals(code.toString())) {
                        JSONObject object = array.getJSONObject(0);

                        _url = object.getString("url");
                        wv_myWebView.loadUrl(_url);

                    } else {
                        Log.e("code", code.toString());
                        Log.e("message", code.toString());
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });










    }

    private void information(){
        JSONArray array = new JSONArray();
        JSONObject object = new JSONObject();

        try {
            object.put("exam_site_id", _id+"");
            array.put(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        MyNet.doPostJsonArray("602", array, new callBackJsonArray() {
            @Override
            public void run(Object code, Object message, JSONArray array) {
                try {
                    if ("200".equals(code.toString())) {
                        JSONObject object = array.getJSONObject(0);
                        String _id = object.getString("id");
                        String _name = object.getString("exam_site_name");
                        String _desc = object.getString("site_introduce");
                        _tel = object.getString("tel");
                        String _img = object.getString("site_img");
//                        tv_desc.setText(_desc);
//                        tv_name.setText(_name);
//                        finalImageLoader.display(iv_imag, _img);
                        //Log.e("url",_img);
                    } else {
                        Log.e("code", code.toString());
                        Log.e("message", code.toString());
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    @Override
    protected void HandleTitleBarEvent(TitleBar component, View v) {

        switch (component) {
            case LEFT:
                finish();
                break;
            case RIGHT:
                popWindow();
                break;

        }

    }

    private void findById() {

        wv_myWebView = (WebView) findViewById(R.id.wv_myWebView);

        // 拨号界面
        //ImageView call = (ImageView) findViewById(R.id.btn_study_where_detail_call);
//        iv_imag = (ImageView) findViewById(R.id.iv_imag);
//        tv_desc = (TextView) findViewById(R.id.tv_desc);
//        tv_name = (TextView) findViewById(R.id.tv_name);

//        call.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                popWindow();
//            }
//        });

    }


    public void popWindow() {
        LayoutInflater mLayoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        View menuView = mLayoutInflater.inflate(R.layout.fragment_study_call_menu, null, true);       //弹出窗口包含的视图
        popupWindow = new PopupWindow(menuView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, true); //创建弹出窗口，指定大小
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        menuView.setFocusableInTouchMode(true);
        menuView.setFocusable(true);
        popupWindow.setBackgroundDrawable(getResources().getDrawable(R.mipmap.study_call_menu_bg));
        menuView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (popupWindow.isShowing()) {
                    popupWindow.dismiss();
                }
            }
        });
        menuView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
                if (arg1 == KeyEvent.KEYCODE_BACK) {
                    if (popupWindow != null) {
                        popupWindow.dismiss();
                    }
                }
                return false;
            }
        });
        popupWindow.update();
        popupWindow.showAtLocation(findViewById(R.id.header_right_btn), Gravity.BOTTOM, 0, 0); // 设置窗口显示的位置

        // 拨打电话
        Button cancel = (Button) menuView.findViewById(R.id.btn_study_call_cancel);
        cancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });

        // 拨打电话
        Button call = (Button) menuView.findViewById(R.id.btn_call_phone_num);
        call.setText(_tel);
        call.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                Intent intent = new Intent(Intent.ACTION_CALL);
                Uri data = Uri.parse("tel:" + _tel);
                intent.setData(data);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onClick(View v) {

    }
}
